<?php

class Api_Controller extends Base_Controller {


	public function action_show()
	{

		return Api::show();
	}



	public function action_index()
	{
		
		if($this->checkSignature()){
			$echostr = $_GET['echostr'];
			echo $echostr;
			exit;
		}


	}
    

    //验证微信是否绑定了url
    private function checkSignature()  
    {  
        $signature = $_GET["signature"];  
        $timestamp = $_GET["timestamp"];  
        $nonce = $_GET["nonce"];              
        $token = 'weixin';  
        $tmpArr = array($nonce, $timestamp, $token);  
        sort($tmpArr);  
        $tmpStr = implode( $tmpArr );  
        $tmpStr = sha1( $tmpStr );  
          
        if( $tmpStr == $signature ){  
            return true;  
        }else{  
            return false;  
        }  

    }

	//创建微信菜单
	public function action_definetiem()
    {	

        $access_token = Api::get_access_token();
        //var_dump($access_token);
        //return $access_token;
        $url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
   		$r = DB::table('wx_button')->get();
        $arrObj = array();
        foreach ($r as $key => $value) {
        	if($value['explanation'] == 0 ) {
	        	$arrObj['button'][$value['sub_buttonid']]['name'] = urlencode($value['button_name']);
	        	$arrObj['button'][$value['sub_buttonid']]['type'] = $value['button_type'];
	        	$arrObj['button'][$value['sub_buttonid']]['url'] = $value['button_url'];
        	}
        }
        foreach ($r as $key => $value) {
        	if($value['explanation'] != 0 ) {
	        	$arrObj['button'][$value['explanation']-1]['sub_button'][$value['sub_buttonid']]['name'] = urlencode($value['button_name']);
	        	$arrObj['button'][$value['explanation']-1]['sub_button'][$value['sub_buttonid']]['type'] = $value['button_type'];
	        	$arrObj['button'][$value['explanation']-1]['sub_button'][$value['sub_buttonid']]['url'] = $value['button_url'];
	        	unset($arrObj['button'][$value['explanation']-1]['url']);
	        	unset($arrObj['button'][$value['explanation']-1]['type']);

        	}
        }


     //    var_dump($arrObj);
     //    exit;
     //    array(
     //    	'button'=>array(
     //    		'1'=>array('name'=>'性格测试','type'=>'view','url'=>'http://persontest.cc/home/getuserinfo'),
     //    		'2'=>array('name'=>'星座分析','sub_button'=>array(array('name'=>'天秤座','type'=>'view','url'=>'https://www.baidu.com/'))),
     //    		'3'=>array('name'=>'塔罗牌预测','type'=>'view','url'=>'http://persontest.cc/home/getuserinfo'),
     //    		),

     //    		);
     //    $arrObj = array(
    	// 	'button'=>array(
	    // 		    array(
				 //        "name"=>urlencode("性格测算"),
     //                    "type"=>'view',
     //                    "url"=>'http://caichuang.ngrok.cc/home/',
			  //         	),
			  //       array(
			  //       	'name'=>urlencode('娱乐'),
			  //       	'sub_button'=>array(
			  //       		array(
			  //       			'name'=>urlencode('笑话'),
			  //       			'type'=>'click',
			  //       			'key'=>'song2',
			  //       			),
			  //       		array(
			  //       			'name'=>urlencode('音乐'),
			  //       			'type'=>'view',
			  //       			'url'=>'http://news.baidu.com/?tn=news',
			  //       			),
			  //       		),
			  //         	),
			  //       array(
			  //       	'name'=>urlencode('百度'),
			  //       	'type'=>'view',
			  //       	'url'=>'http://www.baiducom.cc',
			  //         	)
		   //  	),
    	// );
        $jsonObj = urldecode(json_encode($arrObj));
        $res =Api::http_curl($url,'post','json',$jsonObj);
        return $res['errmsg'];
          
    }








	public function action_get()
   {  
    return Api::get_appid();
   }


   //获取当前微信菜单配置;
   public function action_getdemo()
   {
   	$access_token = Api::get_access_token();
   	$url = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token='.$access_token;
   	$res =Api::http_curl($url);
   	var_dump($res);
   	


   }















}